<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/2/24
 * Time: 14:33
 */

namespace app\admin\controller;
use app\admin\controller\common\Admin;
use app\admin\controller\common\Yan_zheng;
use app\admin\controller\common\ZiXun;
use think\Db;
class Journalism extends admin
{
    //咨询类表
    public function index()
    {
        //获取咨询分类
        if(request() ->isAjax()){
            if(isset($_POST['name']) && !empty($_POST['name'])){//值存在不
                if ($_POST['name'] == 'type') {//返回用户组
                    return $this ->zi_xun();
                }
                else{
                    //验证是不是数字
                    $Yan_zheng = new Yan_zheng();
                    if (!$error = $Yan_zheng ->is_number([$_POST['name']])) {
                        return $error;
                    }
                    //返回数据
                    return Db::table('q_information') ->where('type',$_POST['name'])->select();
                }
            }
        }

        return view('');
    }

    #添加咨询/咨询组
    public function add()
    {
        if(request() ->isAjax()){
            $Yan_zheng = new Yan_zheng();
            if(isset($_POST['type'])){//添加咨询内容
                $da['author'] = input('post.author');
                $da['type'] = input('post.type');
                $da['source'] = input('post.source');
                $da['value'] = input('post.content');
                $da['name'] = input('post.name');
                if(!$error = $Yan_zheng ->is_string([$da['author'],$da['type'],$da['source'],$da['value'],$da['name']])){
                    return $error;
                }
                $state = Db::table('q_information')->insert($da);
                $Yan_zheng ->rizhi("添加咨询,状态:{$state},内容是：".implode(',',$da));
                return $state;
            }
            else{
                $da['name'] = input('post.name');
                //验证是否特殊字符
                if(!$error = $Yan_zheng ->is_string($da)){
                    return $error;
                }
                $state =  Db::table('q_journalism')->insert($da);
                $Yan_zheng ->rizhi("添加咨询组,状态:{$state},内容是：".implode(',',$da));
                return $state;
            }
        }

        return view('');
    }

    #咨询分类
    public function type()
    {
        if (request() ->isAjax()) {
            $Yan_zheng = new Yan_zheng();

            //修改数据
            if(isset($_GET['up'])){
                $state = Db::table('q_journalism') ->where('id',$_GET['id']) ->update(['name'=>$_GET['up']]);
                $Yan_zheng ->rizhi("修改咨询分类,修改成:{$_GET['up']},状态：{$state}");
            } else {
                $state = Db::table('q_journalism') ->where('id',input('id')) ->delete();
                $Yan_zheng ->rizhi("修改删除一个咨询分类,状态：{$state}");
            }
            return $state;

        }
        return view('');
    }

    #删除咨询
    public function de($id)
    {
        $Yan_zheng = new Yan_zheng();
        if(!$error = $Yan_zheng ->is_number([$id])){
            return $error;
        }

        $state = Db::table('q_information') ->delete($id);
        $Yan_zheng ->rizhi("删除一个咨询,状态:{$state},id是：{$id}");
        return $state;
    }

    //修改咨询
    public function up($id)
    {
        $Yan_zheng = new Yan_zheng();
        //修改数据
        if (request() ->isAjax()) {
            $da['id'] = input('post.id');
            $da['author'] = input('post.author');
            $da['type'] = input('post.type');
            $da['source'] = input('post.source');
            $da['value'] = input('post.content');
            $da['name'] = input('post.name');
            if(!$error = $Yan_zheng ->is_string([$da['author'],$da['type'],$da['source'],$da['value'],$da['name']])){
                return $error;
            }
            $state = Db::table('q_information') ->update($da);
            $Yan_zheng ->rizhi("添加咨询,状态:{$state},内容是：".implode(',',$da));
            return $state;
        }

        //验证id

        if(!$error = $Yan_zheng ->is_number([$id])){
            exit('传参错误');
        }
        //获取数据
        $vi['data'] = Db::table('q_information')->where('id',$id)->find();
        if (count($vi['data']) == 0) {
            exit('数据不存在');
        }
        return view('',$vi);
    }

    //获取咨询分类
    public function zi_xun()
    {
        return Db::table('q_journalism')->select();
    }


}